from blls.data_blls.data_bll import DataBLL
from handlers.base_handler import BaseHandler
from utils.check_params_utlil import CheckParams
from utils.common_util import CommonUtil
from utils.handler_util import HandlerUtil


class DataListByProject(BaseHandler):
    def post(self):
        check = CheckParams(params=self.request_data,
                            conditions=[["project_id", True, ""],
                                        ["page_size",False,10],
                                        ["page_num",False,1],
                                        ["is_page",False,0]
                                        ]).check()
        if not check.is_success:
            return self.write(HandlerUtil.set_failure_response(message=check.message))
        res = DataBLL(request=self).get_data_by_project(check.data)
        return self.write(HandlerUtil.set_response(data=res.data, message=res.message, is_success=res.is_success))

class DataListByProject2(BaseHandler):
    def post(self):
        check = CheckParams(params=self.request_data,
                            conditions=[["project_id", True, ""],
                                        ["page_size",False,10],
                                        ["page_num",False,1],
                                        ["is_page",False,0]
                                        ]).check()
        if not check.is_success:
            return self.write(HandlerUtil.set_failure_response(message=check.message))
        res = DataBLL(request=self).get_data_by_project_2(check.data)
        return self.write(HandlerUtil.set_response(data=res.data, message=res.message, is_success=res.is_success))


class DataNumByProject(BaseHandler):
    def post(self):
        check = CheckParams(params=self.request_data,
                            conditions=[["project_id", True, ""],
                                        ]).check()
        if not check.is_success:
            return self.write(HandlerUtil.set_failure_response(message=check.message))
        res = DataBLL(request=self).get_data_num_by_project(check.data)
        return self.write(HandlerUtil.set_response(data=res.data, message=res.message, is_success=res.is_success))


class DataSrarchData(BaseHandler):
    def post(self):
        check = CheckParams(params=self.request_data,
                            conditions=[["project_id", True, ""],
                                        ["data_id", False, ""],
                                        ]).check()
        if not check.is_success:
            return self.write(HandlerUtil.set_failure_response(message=check.message))
        res = DataBLL(request=self).get_data_by_data_id(check.data)
        return self.write(HandlerUtil.set_response(data=res.data, message=res.message, is_success=res.is_success))

class DataSrarchData_2(BaseHandler):
    def post(self):
        check = CheckParams(params=self.request_data,
                            conditions=[["data_id", True, ""],
                                        ]).check()
        if not check.is_success:
            return self.write(HandlerUtil.set_failure_response(message=check.message))
        res = DataBLL(request=self).get_data_by_data_id_prev(check.data)
        return self.write(HandlerUtil.set_response(data=res.data, message=res.message, is_success=res.is_success))


class GetDataImageHandler(BaseHandler):
    def get(self, data_id):
        res = DataBLL(request=self).get_data_image(data_id)
        if not res.is_success:
            return self.write(HandlerUtil.set_response(data=res.data, message=res.message, is_success=res.is_success))
        file_path, file_type, file_name = res.data['file_path'], res.data['image_type'], res.data['file_name']
        self.set_header('Content-Type', file_type)
        self.set_header('Content-Disposition', 'attachment;filename={}'.format(file_name))
        f = CommonUtil.open_file(file_path)
        return self.write(f)

class GetDataImageTrimapHandler(BaseHandler):
    def get(self, data_id):
        res = DataBLL(request=self).get_data_image_trimap(data_id)
        if not res.is_success:
            return self.write(HandlerUtil.set_response(data=res.data, message=res.message, is_success=res.is_success))
        file_path, file_type, file_name = res.data['file_path'], res.data['image_type'], res.data['file_name']
        self.set_header('Content-Type', file_type)
        self.set_header('Content-Disposition', 'attachment;filename={}'.format(file_name))
        f = CommonUtil.open_file(file_path)
        return self.write(f)